package cn.hunqing.web.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.hunqing.web.dto.TemplateSimpleDto;
import cn.hunqing.web.msg.ErrorMsgs;
import cn.hunqing.web.param.TemplateParameters;
import cn.hunqing.web.service.TemplateService;
import cn.hunqing.web.util.PathConstants;
import cn.hunqing.web.util.SessionUtils;
import cn.hunqing.web.util.ViewConstants;

/**
 * add the redirect method
 * 
 * @date 2015-07-27 21:12
 */
@Controller
@RequestMapping(PathConstants.PATH_TEMPLATE_LIST)
public class TemplateController {
	private static Logger logger = LoggerFactory.getLogger(TemplateController.class);

	@Autowired
	private TemplateService templateService;
	
	@RequestMapping(value = "/{version}", method = RequestMethod.GET)
	public String doGetIndex(HttpServletRequest request, HttpSession httpSession,@PathVariable String version,ModelMap model) {
		logger.info("Get the template index request");
		List<TemplateSimpleDto> topTemplates = templateService.getTopTemplates();
		if(topTemplates !=null){
			int size =  topTemplates.size();
			for(int i = 1; i<= size; i++){
				model.put(TemplateParameters.RESPONSE_TOP_TEMPLATES+i, topTemplates.get(i-1));
			}
		}
		model.put(TemplateParameters.LOGIN_FLAG, SessionUtils.isLogin(httpSession));
		model.put(TemplateParameters.RESPONSE_USER_NAME, SessionUtils.getUserName(httpSession));
		return ViewConstants.VIEW_TEMPLATE_LIST;
	}

	/**
	 * return the selected orignal template
	 * 
	 * @param request
	 * @param httpSession
	 * @param templateId
	 * @param model
	 * @return
	 * @throws WxPayException 
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 */
	@RequestMapping(value = "/{templateId}/{version}", method = RequestMethod.GET)
	public String doGetTemplate(HttpServletRequest request, HttpSession httpSession, @PathVariable String templateId,@PathVariable String version,ModelMap model) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		logger.info("Get the template:{} request",templateId);
		templateId = StringUtils.trim(templateId);
		if(!StringUtils.isNumeric(templateId)){
			model.put(TemplateParameters.RESPONSE_ERROR_MSG, ErrorMsgs.WRONG_TEMPLATE_ID);
			return ViewConstants.VIEW_ERROR;
		}
		String templatePath = templateService.getTemplatePath(templateId);
		if(StringUtils.isEmpty(templatePath)){
			model.put(TemplateParameters.RESPONSE_ERROR_MSG, ErrorMsgs.EMPTY_TEMPLATE_PATH);
			return ViewConstants.VIEW_ERROR;
		}
		model.addAttribute(TemplateParameters.RESPONSE_TEMPLATE_ID, templateId);
		return String.format(ViewConstants.VIEW_TEMPLATE, templatePath);
	}
	
	@RequestMapping(value = "/{templateId}/album/{version}", method = RequestMethod.GET)
	public String doGetTemplateAlum(HttpServletRequest request, HttpSession httpSession, @PathVariable String templateId,@PathVariable String version,ModelMap model) {
		logger.info("Get the template:{} request",templateId);
		templateId = StringUtils.trim(templateId);
		if(!StringUtils.isNumeric(templateId)){
			model.put(TemplateParameters.RESPONSE_ERROR_MSG, ErrorMsgs.WRONG_TEMPLATE_ID);
			return ViewConstants.VIEW_ERROR;
		}
		String templatePath = templateService.getTemplatePath(templateId);
		if(StringUtils.isEmpty(templatePath)){
			model.put(TemplateParameters.RESPONSE_ERROR_MSG, ErrorMsgs.EMPTY_TEMPLATE_PATH);
			return ViewConstants.VIEW_ERROR;
		}
		model.addAttribute(TemplateParameters.RESPONSE_TEMPLATE_ID, templateId);
		return String.format(ViewConstants.VIEW_ALBUM, templatePath);
	}


	/**
	 * Return the selected template preview page
	 * 
	 * @param request
	 * @param httpSession
	 * @param templateId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{templateId}/preview/{version}",method=RequestMethod.GET)
	public String doGetTemplatePreview(HttpServletRequest request, HttpSession httpSession, @PathVariable String templateId,@PathVariable String version,ModelMap model) {
		logger.info("Get the wedding Invitation tempplate preview:{}",templateId);
		TemplateSimpleDto templateSimpleDto = templateService.getTemplate(templateId);
		if(templateSimpleDto == null){
			return ViewConstants.VIEW_ERROR;
		}
		model.addAttribute(TemplateParameters.RESPONSE_SELECTED_TEMPLATE, templateSimpleDto);
		return ViewConstants.VIEW_PREVIEW;
	}

	@RequestMapping(value = "/{templateId}/vote",method=RequestMethod.GET)
	public String doGetTemplateVote(HttpServletRequest request, HttpSession httpSession, @PathVariable String templateId,ModelMap model) {
		logger.info("Get the wedding Invitation tempplate vote:{}",templateId);

		Integer userId = SessionUtils.getUserId(httpSession);
		
		if(userId == null){
			return "redirect:"+PathConstants.PATH_LOGIN;
		}
		templateService.vote(templateId,userId);
		return "redirect:"+PathConstants.PATH_TEMPLATE_LIST;
	}
}
